package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.adapter.InforTwoAdapter;
import com.fanhao.dllo.soundfm.entity.InforSubTypeResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.utils.Circlelmage;
import com.fanhao.dllo.soundfm.volley.GetBitmap;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 16/1/19.
 * 轮播 type= 9,subType= 2 时的界面
 */
public class InforSubTypeTwoFragment extends BaseFragment {
    private ListView inforTwoLv;
    private InforTwoAdapter inforTwoAdapter;
    private String specialId;
    private View headerView;
    private ImageView backInforTwoIv;
    private TextView titleHeaderTv, contentHeaderTv, anchorNameHeaderTv;
    private Circlelmage anchorHeaderIv;
    public InforSubTypeTwoFragment(long specialId) {
        this.specialId = String.valueOf(specialId);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_infor_subtype_two;
    }

    @Override
    protected void initView() {
        // 绑定头布局,并初始化
        headerView = LayoutInflater.from(getActivity())
                .inflate(R.layout.fragment_infor_subtype_header, null);
        titleHeaderTv = (TextView) headerView.findViewById(R.id.tvTitle_inforSubType_header);
        contentHeaderTv = (TextView) headerView.findViewById(R.id.tvContent_inforSubType_header);
        anchorNameHeaderTv = (TextView) headerView.findViewById(R.id.tvAnchor_inforSubTypeHeader);
        anchorHeaderIv = (Circlelmage) headerView.findViewById(R.id.ivAnchor_inforSubTypeHeader);

        inforTwoLv = findView(R.id.listView_inforTwo);
        inforTwoLv.addHeaderView(headerView);
        inforTwoAdapter = new InforTwoAdapter(getActivity());
        inforTwoLv.setAdapter(inforTwoAdapter);

    }

    @Override
    protected void initData() {
        String url = "http://mobile.ximalaya.com/m/subject_detail?device=android&id="+specialId+"&position=1&title=%E7%B2%BE%E5%93%81%E5%90%AC%E5%8D%95";
        final StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        InforSubTypeResult result = gson.fromJson(response,
                                InforSubTypeResult.class);
                        if (result != null) {
                            InforSubTypeResult.Info info = result.getInfo();
                                Log.d("InforSubTypeTwoFragment", "llala");
                            if (info != null) {
                                titleHeaderTv.setText(info.getTitle());
                                contentHeaderTv.setText(info.getIntro());
                                anchorNameHeaderTv.setText(info.getNickname());
                                GetBitmap.getBitmap(info.getSmallLogo(), anchorHeaderIv);
                            }
                            List<InforSubTypeResult.ListEntity> list = result.getList();
                            if (list != null && list.size() > 0) {
                                Log.d("InforSubTypeTwoFragment", "list.size():" + list.size());
                                inforTwoAdapter.addInforSubTypeData(list);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }
}
